package cn.aicnn.chatssespringboot.model;

import java.util.ArrayList;
import java.util.List;

import java.util.ArrayList;
import java.util.List;

public class ProjectMetrics {
    public List<FileMetrics> fileMetrics = new ArrayList<>();
    public int totalLines = 0;
    public int totalCommentLines = 0;
    public int totalCyclomaticComplexity = 0;
    public int goodFilesCount = 0; // 好文件数
    public int averageFilesCount = 0; // 中等文件数
    public int poorFilesCount = 0; // 差文件数
    public int externalInputs;      // 外部输入(加权后)
    public int externalOutputs;     // 外部输出(加权后)
    public int externalQueries;     // 外部查询(加权后)
    public int internalLogicalFiles; // 内部逻辑文件
    public int externalInterfaces;  // 外部接口文件

    public void addFileMetrics(FileMetrics fm) {
        fileMetrics.add(fm);
        totalLines += fm.linesOfCode;
        totalCommentLines += fm.commentLines;
        totalCyclomaticComplexity += fm.cyclomaticComplexity;
        this.externalInputs += fm.externalInputs;
        this.externalOutputs += fm.externalOutputs;
        this.externalQueries += fm.externalQueries;
        this.internalLogicalFiles += fm.internalLogicalFiles;
        this.externalInterfaces += fm.externalInterfaces;

        // 计算文件的质量
        fm.calculateCommentDensity();
        fm.assessQuality();

        // 更新质量统计
        if ("Good".equals(fm.consultMetric)) {
            goodFilesCount++;
        } else if ("Average".equals(fm.consultMetric)) {
            averageFilesCount++;
        } else {
            poorFilesCount++;
        }
    }

    public double getCommentDensity() {
        return totalLines == 0 ? 0 : (double) totalCommentLines / totalLines * 100;
    }

    // 获取每种质量类别文件的比例
    public double getGoodFilesPercentage() {
        return (fileMetrics.size() == 0) ? 0 : (double) goodFilesCount / fileMetrics.size() * 100;
    }

    public double getAverageFilesPercentage() {
        return (fileMetrics.size() == 0) ? 0 : (double) averageFilesCount / fileMetrics.size() * 100;
    }

    public double getPoorFilesPercentage() {
        return (fileMetrics.size() == 0) ? 0 : (double) poorFilesCount / fileMetrics.size() * 100;
    }
}

